$(function(){
	_user = getCurrLoginUser();
	customTable = new Custom_table();
	product = new InitAutocomplete();
	product.init({
		"_input":"#product",
		"_hideId":"#product_id",
		"itemChangeCallBack":initProc
	});
	initProduct();
	
	initProductStatus();
	
	staff = new InitAutocomplete();
	staff.init({
		"_input":"#user_name",
		"_hideId":"#user_id",
		"clearDiv":"#proc_name,#proc_id",
		"itemChangeCallBack":initProc
	});
	initStaff();
	proc = new InitAutocomplete();
	proc.init({
		"_input":"#proc_name",
		"_hideId":"#proc_id"
	});
	initProc();
	initPageTable();
})

function initProduct(){
	$.jsonAjax("productServlet","getProduct",null,null,false,function(rtnData){
		if(!isEmpty(rtnData)&&rtnData.data.status=="success"){
			var proList = rtnData.data.content;
			var dataJson = [];
			for(var i=0;i<proList.length;i++){
				dataJson.push({"ID":proList[i].PROD_ID,"VAL":proList[i].PROD_ID,"NAME":proList[i].PROD_NAME});
			}
			product.setOption(dataJson);
		} 
	},null);
}

function initProductStatus(){
	initCodeConfigComponent("#prod_status","#prod_status_id",{"codeType":"prodStatus"},null);
}

function initStaff(){
	$.jsonAjax("procedureServlet","getStaffUser",{},null,false,function(rtnData){
		if(!isEmpty(rtnData)&&rtnData.data.status=="success"){
			var tmpHtml = [];
			var userList = rtnData.data.content;
			var dataJson = [];
			for(var i=0;i<userList.length;i++){
				dataJson.push({"ID":userList[i].USER_ID,"VAL":userList[i].USER_ID,"NAME":userList[i].USER_NAME});
			}
			staff.setOption(dataJson);
		} 
	},null);
}

function initProc(){
	var user_id = $("#user_id").val();
	var prod_id = $("#product_id").val();
	$.jsonAjax("procedureServlet","getProceList",{"USER_ID":user_id,"PROD_ID":prod_id},null,false,function(rtnData){
		if(!isEmpty(rtnData)&&rtnData.data.status=="success"){
			var proList = rtnData.data.content;
			var dataJson = [];
			for(var i=0;i<proList.length;i++){
				dataJson.push({"ID":proList[i].PRO_ID,"VAL":proList[i].PRO_ID,"NAME":proList[i].PRO_NAME});
			}
			proc.setOption(dataJson);
		} 
	},null);
}

function addUserProc(){
	art.dialog.open("addUserProce.html",{
			"title":"修改工序记录",
			"width":"800px",
			"height":"260px"
		},false);
}

//1、查询数据
function queryListData(){
	customTable.initTable();
}

function initPageTable(){
	var mainform = $('#mainform');
	customTable.init({
		table : mainform.find('#listDataTable'),
		tableTbody : mainform.find('#listDataTableTbody'),
		tablePageDiv : mainform.find('#listDataTablePageDiv'),
		columnNum : 11,
		processRowFun : processListDataRow,
		initQueryDataFun : initQueryData
	});	
}

//2、实现：初始化条件数据
function initQueryData() {
	var requestObj = new Object();
	requestObj['url'] = "/ControlServlet/doPost.action";
	requestObj['data'] = new Object();
	requestObj['data']['pageSize'] = customTable.getPageSize();
	requestObj['data']['startNum'] = customTable.getStartNum();
	//定义查询条件
	requestObj['data']['serviceName'] = "procedureServlet";
	requestObj['data']['methodName'] = "getProceNumList";
	requestObj['data']['PROD_ID'] = processUndefined($("#product_id").val());
	requestObj['data']['PROD_STATUS_CD'] = processUndefined($("#prod_status_id").val());
	requestObj['data']['USER_ID'] = processUndefined($("#user_id").val());
	requestObj['data']['PRO_ID'] = processUndefined($("#proc_id").val());
	requestObj['data']['BEGIN_DATE'] = processUndefined($("#begin_date").val());
	requestObj['data']['END_DATE'] = processUndefined($("#end_date").val());
	return requestObj;
}

//3、实现：处理行数据方法
function processListDataRow( seq , tempBean) {
	var rowHtml = [];
	rowHtml.push('<td align="center">' + processUndefined(tempBean.PROD_NAME) + '&nbsp;</td>');
	rowHtml.push('<td align="center">' + processUndefined(tempBean.PROD_STATUS_NAME) + '&nbsp;</td>');
	rowHtml.push('<td align="center">' + processUndefined(tempBean.USER_NAME) + '&nbsp;</td>');
	rowHtml.push('<td align="center">' + processUndefined(tempBean.PRO_NAME) + '&nbsp;</td>');
	rowHtml.push('<td align="center">' + processUndefined(tempBean.PRN_STATUS_NAME) + '&nbsp;</td>');
	rowHtml.push('<td align="center">'+ processUndefined(tempBean.NUM) +'&nbsp;</td>');
	rowHtml.push('<td align="center">'+ initHandler(tempBean)+'&nbsp;</td>');
	return rowHtml.join("");
}

//操作权限控制
function initHandler(tempBean){
	var rtnHtml = [];
	if(_user){
		rtnHtml.push('<a onclick="showProc('+processUndefined(tempBean.PRO_ID)+','+processUndefined(tempBean.PROD_ID)+','+processUndefined(tempBean.USER_ID)+')" class="" >查看</a>&nbsp;');
		//管理员
		if("0"==_user.type){
			if("1001"==processUndefined(tempBean.PRN_STATUS_CD)){
				rtnHtml.push('<a onclick="passProc('+processUndefined(tempBean.PRO_ID)+','+processUndefined(tempBean.PROD_ID)+','+processUndefined(tempBean.USER_ID)+')" class="" >通过</a>&nbsp;');
			}
			rtnHtml.push('<a onclick="modifyProc('+processUndefined(tempBean.PRO_ID)+','+processUndefined(tempBean.PROD_ID)+','+processUndefined(tempBean.USER_ID)+')" class="" >修改</a>&nbsp;');
			rtnHtml.push('<a onclick="delProc('+processUndefined(tempBean.PRO_ID)+','+processUndefined(tempBean.PROD_ID)+','+processUndefined(tempBean.USER_ID)+')" class="" >删除</a>');
		} else{//非管理员
			if("1001"==processUndefined(tempBean.PRN_STATUS_CD)){
				rtnHtml.push('<a onclick="modifyProc('+processUndefined(tempBean.PRO_ID)+','+processUndefined(tempBean.PROD_ID)+','+processUndefined(tempBean.USER_ID)+')" class="" >修改</a>&nbsp;');
				rtnHtml.push('<a onclick="delProc('+processUndefined(tempBean.PRO_ID)+','+processUndefined(tempBean.PROD_ID)+','+processUndefined(tempBean.USER_ID)+')" class="" >删除</a>');
			}
		}
		
	}
	return rtnHtml.join("");
}

function delProc(proc_id,prod_id,user_id){
	art.dialog.isSubmit("是否删除此工序明细记录？",function(){
		var data = {
				"oper_type":"del",
				"status_cd":"9999",
				"user_id":user_id,
				"prod_id":prod_id,
				"proc_id":proc_id
				
		}
		$.jsonAjax("procedureServlet","updateProcedureNum",data,null,false,function(rtnData){
			if(!isEmpty(rtnData)&&rtnData.data.status=="success"){
				art.dialog.message("succeed","删除工序明细记录成功！",function(){
					queryListData();
				});
			} else{
				art.dialog.message("error","删除工序明细记录失败！",null);
			}
		});
	},null);
}

function modifyProc(proc_id,prod_id,user_id){
	var param = "?proc_id="+proc_id+"&prod_id="+prod_id+"&user_id="+user_id;
	art.dialog.open("modifyProce.html"+param,{
			"title":"修改工序明细记录",
			"width":"800px",
			"height":"260px"
		},false);
}
function showProc(proc_id,prod_id,user_id){
	var param = "?proc_id="+proc_id+"&prod_id="+prod_id+"&user_id="+user_id+"&isShow=true";
	art.dialog.open("modifyProce.html"+param,{
		"title":"查看工序明细记录",
		"width":"800px",
		"height":"260px"
	},false);
}

function passProc(proc_id,prod_id,user_id){
	art.dialog.isSubmit("是否审核通过此工序明细记录？",function(){
		var data = {
				"oper_type":"pass",
				"status_cd":"1000",
				"user_id":user_id,
				"prod_id":prod_id,
				"proc_id":proc_id
				
		}
		$.jsonAjax("procedureServlet","updateProcedureNum",data,null,false,function(rtnData){
			if(!isEmpty(rtnData)&&rtnData.data.status=="success"){
				art.dialog.message("succeed","审核通过工序明细记录成功！",function(){
					queryListData();
				});
			} else{
				art.dialog.message("error","审核通过工序明细记录失败！",null);
			}
		});
	},null);
}
